package m09;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import m07.Player;
import m07.Ship;
import m07.GamePanel;


/**
 * A trader, which is a subclass of player and has a ship.
 * @author kateharlan
 *
 */
public class Trader extends Player{
	private static final long serialVersionUID = 7162320032191162270L;
	private Ship tradeShip;
	private int pPilot, pFight, pTrade, pEng;
	private Icon shipIcon;
	
	/**
	 * a basic filler constructor
	 * @param name the traders name
	 * @param pilot the traders pilot points
	 * @param fighter the traders fighter points
	 * @param trader the traders trader points
	 * @param eng the traders engineer points
	 */
	public Trader(String name, int pilot, int fighter, int trader, int eng) {
		super(name, pilot, fighter, trader, eng);
	}
	
	/**
	 * a constructor that takes in a name and sets up
	 * the trader to all random values.
	 * @param name
	 */
	public Trader(String name) {
		this(name, 0,0,0,16);
		Random newNum = new Random();
		pPilot = newNum.nextInt(16);
		this.setPilot(pPilot);
		pFight = newNum.nextInt(16-pPilot);
		this.setFighter(pFight);
		pTrade = newNum.nextInt(16-pPilot-pFight);
		this.setEngineer(pTrade);
		pEng = newNum.nextInt(16-pPilot-pFight-pTrade);
		this.setEngineer(pEng);
		tradeShip = new Ship("TraderShip", 40);
		String[] possGoods = new String[10];
		possGoods[0] = "water";
		possGoods[1] = "furs";
		possGoods[2] = "food";
		possGoods[3] = "ore";
		possGoods[4] = "games";
		possGoods[5] = "firearms";
		possGoods[6] = "medicine";
		possGoods[7] = "machines";
		possGoods[8] = "narcotics";
		possGoods[9] = "robots";
		Random randGood = new Random();
		for (int i = 0; i<30; i++){
			int curGood = randGood.nextInt(10);
			tradeShip.addCargo(tradeShip.goodConverter(possGoods[curGood]));
		}
	}
	
	/**
	 * allows the player to fight with the trader.
	 * the fight is based on the fighter points and randomness.
	 * if the player wins, they get money
	 * if they lose, they lose money
	 * otherwise nothing happens
	 * @return the updated player
	 */
	public void fight(){
		BufferedImage ship;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("ship.png");
			ship = ImageIO.read(input);
			shipIcon = new ImageIcon(ship);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Icon Not Loaded");
		}
		Random fight = new Random();
		int curFighter = Global.currentPlayer.getFighter();
		curFighter = (int)(fight.nextFloat()*curFighter + curFighter);
		if (getFighter() > Global.currentPlayer.getFighter()){
			JOptionPane.showMessageDialog(null, "You lost the fight! The trader took 100 credits of your money", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			Global.currentPlayer.setMoney(Global.currentPlayer.getMoney()-100);
		}
		else if (getFighter() == Global.currentPlayer.getFighter()){
			JOptionPane.showMessageDialog(null, "The fight was a draw. You both go on your seperate ways.", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			Global.currentPlayer.setFighter(Global.currentPlayer.getFighter()+1);
		}
		else {
			JOptionPane.showMessageDialog(null, "You won the fight! The trader gave you 100 credits!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			Global.currentPlayer.setMoney(Global.currentPlayer.getMoney() + 100);
			Global.currentPlayer.setFighter(Global.currentPlayer.getFighter()+1);
		}
	}
	
	/**
	 * allows the player to flee from a trader.
	 * @param curPlayer the player that flees.
	 * @return the player
	 */
	public void flee(GamePanel gp){
		BufferedImage ship;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("ship.png");
			ship = ImageIO.read(input);
			shipIcon = new ImageIcon(ship);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Icon Not Loaded");
		}
		Random flee = new Random();
		int curFlee = Global.currentPlayer.getPilot();
		curFlee = (int)(flee.nextFloat()*curFlee + curFlee);
		if (getPilot() > Global.currentPlayer.getPilot()){
			JOptionPane.showMessageDialog(null, "You were too slow! Now you must fight!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			JOptionPane.showMessageDialog(null, "You were too slow! Now you must fight!", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			fight();
		}
		else if (getPilot() == Global.currentPlayer.getPilot()){
			JOptionPane.showMessageDialog(null, "The fight was a draw. You decide to trade.", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			trade(gp);
			Global.currentPlayer.setPilot(Global.currentPlayer.getPilot()+1);
		}
		else {
			JOptionPane.showMessageDialog(null, "You got away.", "Message", JOptionPane.INFORMATION_MESSAGE, shipIcon);
			Global.currentPlayer.setPilot(Global.currentPlayer.getPilot()+2);
		}
		//return Global.currentPlayer;
	}
	
	/**
	 * Allows the trader to trade with a player.
	 * @return the player
	 */
	public void trade(GamePanel gp){
		JFrame tradeFrame = new JFrame();
		tradeFrame.setResizable(false);
		tradeFrame.setTitle("Trading with a Trader");
		tradeFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		tradeFrame.setAlwaysOnTop(true);
		
		TradePanel tp = new TradePanel(this, gp, tradeFrame);
		tradeFrame.setSize(new Dimension(250, 320));
		tradeFrame.setContentPane(tp);
		tp.setLayout(null);
		tp.setVisible(true);
		
		tradeFrame.setVisible(true);
		Global.currentPlayer.setTrader(Global.currentPlayer.getTrader()+1);
		//return Global.currentPlayer;
	}
}
